package com.zmy.xcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmy.xcx.common.convention.exception.ClientException;
import com.zmy.xcx.common.enums.UserErrorCodeEnum;
import com.zmy.xcx.dao.entity.MedicationCategory;
import com.zmy.xcx.dao.mapper.MedicationCategoryMapper;
import com.zmy.xcx.service.MedicationCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 药品分类服务实现类
 */
@Slf4j
@Service
public class MedicationCategoryServiceImpl extends ServiceImpl<MedicationCategoryMapper, MedicationCategory> implements MedicationCategoryService {
    
    @Override
    public MedicationCategory getMedicationCategory(Integer id) {
        log.info("开始查询药品分类信息，id: {}", id);
        try {
            LambdaQueryWrapper<MedicationCategory> queryWrapper = Wrappers.lambdaQuery(MedicationCategory.class)
                    .eq(MedicationCategory::getId, id);
            
            MedicationCategory category = baseMapper.selectOne(queryWrapper);
            
            if (category == null) {
                log.warn("未找到对应的药品分类信息，id: {}", id);
                throw new ClientException(UserErrorCodeEnum.USER_NULL);
            }
            
            log.info("查询药品分类信息成功，id: {}", id);
            return category;
        } catch (ClientException e) {
            throw e;
        } catch (Exception e) {
            log.error("查询药品分类信息失败，id: {}", id, e);
            throw new ClientException("查询药品分类信息失败");
        }
    }

    @Override
    public MedicationCategory getMedicationCategoryByName(String name) {
        log.info("开始根据名称查询药品分类信息，name: {}", name);
        try {
            MedicationCategory category = baseMapper.findByName(name);
            
            if (category == null) {
                log.warn("未找到对应的药品分类信息，name: {}", name);
                throw new ClientException(UserErrorCodeEnum.USER_NULL);
            }
            
            log.info("查询药品分类信息成功，name: {}", name);
            return category;
        } catch (ClientException e) {
            throw e;
        } catch (Exception e) {
            log.error("查询药品分类信息失败，name: {}", name, e);
            throw new ClientException("查询药品分类信息失败");
        }
    }
} 